import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutterdemo/2house730page/event/events.dart';

class BannerSearchWidget extends StatefulWidget {
  final bool show;
  final double scrollOffset;

  BannerSearchWidget({this.show, this.scrollOffset});

  @override
  State<StatefulWidget> createState() {
    return _BannerSearchWidgetState();
  }
}

class _BannerSearchWidgetState extends State<BannerSearchWidget> {
  final double _bannerHeight = 500.h;
  final double _searchHeight = 140.h;
  double scrollOffset = 0;

  @override
  void initState() {
    super.initState();

    eventBus.on<MainPageScrollEvent>().listen((event) {
      setState(() {
        scrollOffset = event.scrollOffset;
      });
    });
  }

  double _getOpacity() {
    if (scrollOffset <= 0) {
      return 1.0;
    } else if (scrollOffset > 0 && scrollOffset < 95.h) {
      return 1 - (scrollOffset / 95.h);
    } else {
      return 0;
    }
  }

  @override
  Widget build(BuildContext context) {
    if (widget.show) {
      return Container(
        height: _bannerHeight + _searchHeight / 2,
        child: Stack(
          overflow: Overflow.clip,
          children: <Widget>[
            Container(
              height: _bannerHeight,
              child: Swiper(
                itemBuilder: (BuildContext context, int index) {
                  return new Image.network(
                    "http://pic.sc.chinaz.com/files/pic/pic9/202005/apic25333.jpg",
                    fit: BoxFit.cover,
                  );
                },
                itemCount: 3,
                loop: true,
                autoplay: true,
                pagination: new SwiperPagination(
                    builder: DotSwiperPaginationBuilder(
                        size: 10.w, activeSize: 10.w, activeColor: Colors.red, color: Colors.black26),
                    margin: EdgeInsets.only(bottom: 70.h)),
              ),
            ),
            Opacity(
              opacity: _getOpacity(),
              child: Container(
                margin: EdgeInsets.only(left: 30.w, right: 30.w, top: _bannerHeight - (_searchHeight / 2)),
                height: _searchHeight,
                child: GestureDetector(
                  onTap: _search,
                  child: Card(
                    elevation: 10.h,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Icon(Icons.search),
                        Text("搜寻你的心水楼盘"),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      );
    } else {
      return Container(
//        height: _bannerHeight + _searchHeight / 2 - 10.h,
        height: _bannerHeight + _searchHeight / 2,
      );
    }
  }

  void _search() {
    print("search");
  }
}
